package com.service.oos;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.dao.oos.OosItemDao;
import com.model.oos.OosItemModel;
import com.common.model.Global;

@Service
public class OosItemService {
    static final Logger logger = LoggerFactory.getLogger(OosItemService.class);

    @Autowired
    OosItemDao oosItemDao;

    /**
     * 列表查询
     *
     * @param queryMap
     * @return
     */
    public HttpResult selectList(Map<String, Object> queryMap) {
        HttpResult httpResult = new HttpResult();
        EasyuiUtils.addPageNum(queryMap);
        EasyuiUtils.addFilter(queryMap);
        List<OosItemModel> list = oosItemDao.selectList(queryMap);
        int count = oosItemDao.selectListCount(queryMap);
        httpResult.setRows(list);
        httpResult.setTotal(count);
        logger.info("selectList:{}", list);
        return httpResult;
    }

    /**
     * 详情
     *
     * @param
     * @return
     */
    public HttpResult selectInfor(String itemId) {
        HttpResult httpResult = new HttpResult();
        OosItemModel oosItemModel = oosItemDao.load(itemId);
        httpResult.setData(oosItemModel);
        httpResult.setCode(HttpStatus.SUCCESS_CODE);
        return httpResult;
    }

    /**
     * 保存OOS异常项
     *
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public HttpResult save(OosItemModel oosItemModel) {
        if (oosItemModel == null) {
            return HttpResult.error("参数不能为空");
        }
        HttpResult httpResult = new HttpResult();
        int result = 0;
        if (!StringUtils.isNotNull(oosItemModel.getItemId())) {
            oosItemModel.setItemId(UUID.randomUUID().toString());
            oosItemModel.setStatus("0");
            result = oosItemDao.insert(oosItemModel);
        } else {
            result = oosItemDao.update(oosItemModel);
        }
        if (result > 0) {
            httpResult.setMsg(oosItemModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
            httpResult.setData(oosItemModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg(oosItemModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
        }
        return httpResult;
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    public HttpResult delete(String itemId) {
        HttpResult httpResult = new HttpResult();
        if (!StringUtils.isNotNull(itemId)) {
            return HttpResult.error("参数不能为空");
        }
        int result = oosItemDao.delete(itemId);
        if (result > 0) {
            httpResult.setMsg("删除成功！");
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg("不存在此条数据！");
        }
        return httpResult;
    }

    /**
     * 导出
     *
     * @param queryMap
     * @param response
     */
    public void export(Map<String, Object> queryMap, HttpServletResponse response) {
        List<OosItemModel> list = oosItemDao.selectList(queryMap);
        logger.info("export:{}", list);
        try {
            EasyExcel.write(response.getOutputStream(), OosItemModel.class).sheet("模板").doWrite(list);
        } catch (IOException e) {
            logger.error("导出错误:{}", e.getMessage());
        }

    }
}
